import datetime
from datetime import timedelta

# 时间戳 & datetime.datetime对象 & 格式化字符串 三者时间的转换

# 1.时间戳 and datetime.datetime 之间的转换
now1 = datetime.datetime.now(tz=None)
print(now1)
print(now1.timestamp())
now2 = now1.timestamp()
print(datetime.datetime.fromtimestamp(now2, tz=None))

# 2.datetime.datetime and 格式化字符串 之间的转换
now3 = now1
print(now3.strftime("%Y-%m-%d %H:%M:%S"))
now4 = now3.strftime("%Y-%m-%d %H:%M:%S")
print(datetime.datetime.strptime(now4, "%Y-%m-%d %H:%M:%S"))

# 3.时间戳 and 格式化字符串 之间的转换
now5 = now1.timestamp()  # 获得时间戳
now6 = datetime.datetime.fromtimestamp(now5, tz=None)  # 时间戳转化为datetime.datetime
print(now6.strftime("%Y-%m-%d %H:%M:%S"))  # datetime.datetime转化为格式化字符串


def get_date(day_delta):
    now = datetime.datetime.now(tz=None)
    now = now + timedelta(days=day_delta)
    now = now.strftime("%Y-%m-%d")
    return now

print(get_date(-1))